home *** CD-ROM | disk | FTP | other *** search
- unit clLockWindow;
- interface
- uses Windows,Classes,Forms,Controls,Messages;
- type
- TLockedItem = Class
- private
- fControl: TWinControl;
- fHandle: THandle;
- fRefCount: integer;
- protected
- procedure SetLock; virtual;
- procedure ResetLock; virtual;
- public
- constructor Create(AControl: TWinControl);
- destructor Destroy; override;
- property Control: TWinControl read fControl;
- property RefCount: integer read fRefCount write fRefCount;
- end;
- TLockWindow = Class
- private
- fList: TList;
- fCursor: TCursor;
- protected
- procedure DeleteLock(index: integer); virtual;
- procedure SetCursor; virtual;
- procedure ResetCursor; virtual;
- function UpdateCount(index,delta: integer): Integer; virtual;
- public
- constructor Create; virtual;
- destructor Destroy; override;
- function IndexOf(Item: Pointer): Integer; virtual;
- procedure Lock(AControl: TWinControl); virtual;
- procedure Unlock(AControl: TWinControl); virtual;
- procedure UnlockAll; virtual;
- end;
- implementation
- constructor TLockedItem.Create(AControl: TWinControl);
- begin
- inherited Create;
- fControl := AControl;
- if fControl=nil then fHandle := 0
- else fHandle := fControl.Handle;
- SetLock;
- end;
-
- destructor TLockedItem.Destroy;
- begin
- ResetLock;
- inherited Destroy;
- end;
- procedure TLockedItem.SetLock;
- begin
- if fHandle <> 0 then SendMessage(fHandle,WM_SETREDRAW,0,0);
- fRefCount := 1;
- end;
- procedure TLockedItem.ResetLock;
- begin
- if fHandle <> 0 then begin
- SendMessage(fHandle,WM_SETREDRAW,1,0);
- RedrawWindow(fHandle,nil,0,RDW_FRAME+RDW_INVALIDATE
- +RDW_ALLCHILDREN+RDW_NOINTERNALPAINT);
- end;
- end;
- constructor TLockWindow.Create;
- begin
- inherited Create;
- fList := TList.Create;
- end;
- destructor TLockWindow.Destroy;
- begin
- UnlockAll;
- fList.Free;
- inherited Destroy;
- end;
- procedure TLockWindow.SetCursor;
- begin
- fCursor := Screen.Cursor;
- Screen.Cursor := crHourGlass;
- end;
- procedure TLockWindow.ResetCursor;
- begin
- Screen.Cursor := fCursor;
- end;
- function TLockWindow.UpdateCount(index,delta: integer): Integer;
- var item: TLockedItem;
- begin
- item := TLockedItem(fList[index]);
- item.RefCount := item.RefCount+delta;
- Result := item.RefCount;
- end;
- function TLockWindow.IndexOf(Item: Pointer): Integer;
- begin
- for Result := 0 to fList.Count-1 do
- if TLockedItem(fList[Result]).Control=Item then exit;
- Result := -1;
- end;
- procedure TLockWindow.Lock(AControl: TWinControl);
- var ix: integer;
- begin
- if fList.Count=0 then SetCursor;
- ix := IndexOf(AControl);
- if ix < 0 then fList.Add(TLockedItem.Create(AControl))
- else UpdateCount(ix,1);
- end;
- procedure TLockWindow.DeleteLock(index: integer);
- begin
- TLockedItem(fList[index]).Free;
- fList.Delete(index);
- if fList.Count=0 then ResetCursor;
- end;
- procedure TLockWindow.Unlock(AControl: TWinControl);
- var ix: integer;
- begin
- ix := IndexOf(AControl);
- if (0 <= ix) and (UpdateCount(ix,-1) < 1) then DeleteLock(ix);
- end;
- procedure TLockWindow.UnlockAll;
- var ix: integer;
- begin
- for ix := fList.Count-1 downto 0 do DeleteLock(ix);
- end;
- end.
-